package com.example.controller;


import com.example.common.result.Result;
import com.example.entity.dto.weixiubaoyang.MoldAllocationAddDto;
import com.example.entity.dto.weixiubaoyang.MoldAllocationUpdateDto;
import com.example.entity.dto.weixiubaoyang.MoldDto;
import com.example.entity.vo.CommonPageVo;
import com.example.entity.vo.MoldAllocationPrintVo;
import com.example.entity.vo.MoldAllocationVo;
import com.example.service.MoldAllocationDetailService;
import com.example.service.MoldAllocationHeaderService;
import com.example.untils.StringUtils;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author LiangPandeng
 * @since 2024-11-27
 */
@RestController
@RequestMapping("/api/mold-allocation")
@Api(tags = "模具调拨模块")
public class MoldAllocationController {

    @Autowired
    private MoldAllocationDetailService moldAllocationDetailService;

    @Autowired
    private MoldAllocationHeaderService moldAllocationHeaderService;

    //查询
    @PostMapping("/list")
    @ApiOperation("查询模具调拨")
    public Result<CommonPageVo<MoldAllocationVo>> getMoldAllocationVo(@RequestBody MoldDto moldDto){
        if (StringUtils.isEmpty(moldDto.getOrderType())) moldDto.setOrderType("MT");
        return Result.ok(moldAllocationHeaderService.getMoldAllocationVo(moldDto));
    }

    //打印
    @GetMapping("/print")
    @ApiOperation("查询要打印的模具信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderNumber", value = "单号", dataType = "string", dataTypeClass = String.class)
    })
    public Result<MoldAllocationPrintVo> print(@RequestParam String orderNumber){
        MoldAllocationPrintVo vo = moldAllocationHeaderService.print(orderNumber);
        return Result.ok(vo);
    }

    @PostMapping("/add")
    @ApiOperation("新增调拨单")
    public Result addMoldAllocation(@RequestBody MoldAllocationAddDto dto) {
        if(StringUtils.isEmpty(dto.getApplicant()) || StringUtils.isEmpty(dto.getToUserId()))
            return Result.error("申请人和收货人不能为空!");
        if (dto.getNeedReturn().equals("Y") && StringUtils.isEmpty(dto.getReturnDate()))
            return Result.error("需要归还，归还日期必须存在!");
        else if (dto.getNeedReturn().equals("Y"))
            dto.setReturnDate(dto.getReturnDate()
                    .replaceAll("-",""));
        String res =moldAllocationDetailService.addMoldAllocation(dto);
        return Result.success(res);
    }

    @PostMapping("/update")
    @ApiOperation("修改")
    public Result updateMoldAllocation(@RequestBody MoldAllocationUpdateDto dto) {
        if (dto.getNeedReturn().equals("Y") && StringUtils.isEmpty(dto.getReturnDate()))
            return Result.error("需要归还，归还日期必须存在!");
        else if (dto.getNeedReturn().equals("Y"))
            dto.setReturnDate(dto.getReturnDate()
                    .replaceAll("-",""));
        String res =moldAllocationDetailService.updateMoldAllocation(dto);
        return Result.success(res);
    }

    @GetMapping("/deleteMold")
    @ApiOperation("删除调拨单中的某个模具")
    public Result deleteMold(@RequestParam Integer id) {
        return Result.success(moldAllocationDetailService.deleteMold(id));
    }

    @GetMapping("/updateStatus")
    @ApiOperation("调拨单审核相关操作")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "int", dataTypeClass = Integer.class),
            @ApiImplicitParam(name = "type", value = "0-作废  1-收货方确认  2-取消收货确认",
                    dataType = "int", dataTypeClass = Integer.class)
    })
    public Result<?> updateStatus(@RequestParam Integer id,@RequestParam Integer type) {
        moldAllocationHeaderService.updateStatus(id,type);
        return Result.success();
    }

//    @PostMapping("/audit")
//    @ApiOperation("调拨单审核")
//    public Result updateMoldAllocation(@RequestBody MoldApplyDto dto) {
//        String res = moldAllocationDetailService.updateMoldAllocationDetail(dto);
//        return Result.success(res);
//    }

//    //同意或拒绝
//    @PostMapping("/updateMoldAllocationHeader")
//    @ApiOperation("单头同意或拒绝")
//    public Result updateMoldAllocation(@RequestBody MoldApplyDto moldApplyDto) {
//        String res =moldAllocationHeaderService.updateMoldAllocationHeader(moldApplyDto);
//        return Result.success(res);
//    }

}
